package com.foreknow.dao.impl;

import com.foreknow.dao.BaseDao;
import com.foreknow.dao.userDao;
import com.foreknow.model.User;
import com.foreknow.util.DbHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class userDaoImpl implements userDao {

  private  static  QueryRunner queryRunner=DbHelper.getQueryRunner();
    @Override
    public int delete(String sql, Object... params) throws SQLException {
        return queryRunner.update(sql,params);
    }

    @Override
    public int delete(String sql) throws SQLException {
        return queryRunner.update(sql);
    }

    @Override
    public int insert(String sql, Object[] params) throws SQLException {
       queryRunner = DbHelper.getQueryRunner();
        int isRight = 0;
        for (Object value:params) {
            User user = (User) value;
            isRight=queryRunner.update(sql,user.getUsername(),user.getAccount(),user.getPassword(),user.getPhone(),user.getEmail(),user.getAvatar());
        }
        return isRight;


    }

    @Override
    public List queryByCondition(String sql) throws SQLException {
        return queryRunner.query(sql,getListHandler());
    }

    @Override
    public List queryByCondition(String sql, Object... params) throws SQLException {
        return queryRunner.query(sql,getListHandler(),params);
    }

    @Override
    public User queryOne(String sql, Object params) throws SQLException {
        return queryRunner.query(sql,getResultSetHandler(),params);
    }

    @Override
    public User queryOne(String sql, Object... params) throws SQLException {

        return queryRunner.query(sql,getResultSetHandler(),params);
    }

    public  ResultSetHandler<List<User>>getListHandler(){
        ResultSetHandler handler=new ResultSetHandler<List<User>>() {

            @Override
            public List<User> handle(ResultSet resultSet) throws SQLException {
                List<User> users = new ArrayList<>();
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    String username = resultSet.getString("username");
                    String account = resultSet.getString("account");
                    String password = resultSet.getString("password");
                    String phone = resultSet.getString("phone");
                    String email = resultSet.getString("email");
                    String avatar = resultSet.getString("avatar");
                    User user = new User(id, username, account, password, phone, email, avatar);
                    users.add(user);
                }
                return users;
            }

        };
        return handler;
 }


    @Override
    public int update(String sql, Object... params) throws SQLException {
        return queryRunner.update(sql,params);
    }

    @Override
    public int update(String sql, Object params) throws SQLException {
        return queryRunner.update(sql,params);
    }

    @Override
    public ResultSetHandler<User> getResultSetHandler() {
        ResultSetHandler<User> handler=new ResultSetHandler<User>(){

            @Override
            public User handle(ResultSet resultSet) throws SQLException {
                User user=null;
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    String username = resultSet.getString("username");
                    String account = resultSet.getString("account");
                    String password = resultSet.getString("password");
                    String phone = resultSet.getString("phone");
                    String email = resultSet.getString("email");
                    String avatar = resultSet.getString("avater");
                     user = new User(id, username, account, password, phone, email, avatar);

                }
                return user;
            }
        };
        return handler;
    }


}
